import React, { Component, PropTypes } from 'react'
import AuditApplicationDetail from 'containers/financial-management/new-finance-audit/audit-application-detail'
import ApproveExpenseReportDetail from "containers/approve/expense-report/approve-expense-report-detail";
import {getQueryUrlParam, messages} from "share/common";
import {Affix, Button} from "antd";
import GenerateBatch from "containers/financial-management/expense-view/generate-batch";
import ButtonPrintCertificate from "containers/expense-report/components/button-print-certificate";
import ButtonsBarBottom from "containers/expense-report/components/buttons-bar-bottom";
import Condition from "components/condition";
const { If } = Condition;

class CorporatePaymentReportDetailBar extends Component {

  afterClose = refresh => {
    if(refresh) {
      this.props.afterCheckOrAuto(refresh);
    }
  };

  handleRefreshHistory = flag => flag && this.props.getHistory();

  static contextTypes = {
    info: PropTypes.object,
    form: PropTypes.object,
    invoiceEnabled: PropTypes.bool,
    approvalHistory: PropTypes.array,
    isAuditInApproveStatus: PropTypes.bool,
    isWaitForAudit: PropTypes.bool,
    entry: PropTypes.string,
    showApproveBottom: PropTypes.bool,
    approvalChains: PropTypes.array,
  };

  render() {
    const {
      goBack, selectedExpense, selectedInvoice, failedInvoices, expenseReportInvoices, onSelectExpense,
      approverOID, submitting, tab, readOnly
    } = this.props;
    const { info, invoiceEnabled, approvalHistory, isAuditInApproveStatus, isWaitForAudit, form, entry,
      showApproveBottom, auditCapability, approvalChains } = this.context;
    let adminApprove = entry === 'processPlatform' || location.pathname.indexOf('admin-approve-expense-detail') > -1;
    let pay = location.pathname.indexOf('confirm-payment') > -1;
    return <div>
      {
        (entry === 'approve' || adminApprove) && !readOnly && (
          <ApproveExpenseReportDetail
            info={info}
            showApproveBottom={showApproveBottom || adminApprove}
            selectedExpense={selectedExpense}
            customFormPropertyMap={form.customFormPropertyMap}
            expenseReportInvoices={expenseReportInvoices}
            approvalChains={approvalChains}
            approvalHistory={approvalHistory}
            afterClose={goBack}
            invoiceEnabled={invoiceEnabled}
            failedInvoices={failedInvoices}
            goBack={goBack}
            approverOID={approverOID || getQueryUrlParam('approverOID')}
            tab={tab}
            approvalMode={getQueryUrlParam('approvalMode')}
            auditCapability={auditCapability}
          />
        )
      }
      {
        entry === 'audit' && !readOnly && (
          <AuditApplicationDetail
            formOID={info.formOID}
            isAuditInApproveStatus={isAuditInApproveStatus}
            entityOID={info.expenseReportOID}
            status={info.status}
            companyOid={info.docCompanyOID}
            applicantOID={info.applicantOID}
            entityType={1002}
            goBack={goBack}
            customFormPropertyMap={form.customFormPropertyMap}
            expenseOid={info.expenseReportOID}
            businessCode={info.businessCode}
            afterClose={this.afterClose}
            invoiceEnabled={invoiceEnabled}
            approvalHistory={approvalHistory}
            fullWidth
            selectedExpense={selectedExpense || []}
            selectedInvoice={selectedInvoice || []}
            expenseReportInvoices={expenseReportInvoices}
            failedInvoices={failedInvoices}
            isWaitForAudit={isWaitForAudit}
            onSelectExpense={onSelectExpense}
            formType={form.formType}
            info={info}
          />
        )
      }
      {
        entry === 'view' && !readOnly && (
          <Affix offsetBottom={0} className="bottom-bar">
            <GenerateBatch
              mode='single'
              params={{entityOID: info.expenseReportOID, entityType: 1002}}
              isExpenseReportView
              btnProps={{
                name: messages('common.print')/*打印*/,
              }}
            />
            <ButtonPrintCertificate businessCode={info.businessCode} parentBusinessCode={info.parentBusinessCode} appCode={'Exp_Report_Print_Doc'}/>
            <If value={getQueryUrlParam('from') !== 'request'}>
              <Button
                style={{marginLeft: 10}}
                className="back-btn"
                onClick={this.props.goBack}
              >
                {messages('common.back')/*返回*/}
              </Button>
            </If>
          </Affix>
        )
      }
      {
        (pay || readOnly) && (
          <Affix offsetBottom={0} className="bottom-bar">
            <Button className="back-btn" onClick={this.props.goBack}>{messages('common.back')/*返回*/}</Button>
          </Affix>
        )
      }
      {
        entry === 'my' && !readOnly && (
          <ButtonsBarBottom
            form={form}
            info={info}
            approvalChains={approvalChains}
            expenseReportInvoices={expenseReportInvoices}
            submitting={submitting}
            from={getQueryUrlParam('from')}
            refreshHistory={this.handleRefreshHistory}
            onSubmit={this.props.handleSubmit}
            goBack={this.props.goBack}
          />
        )
      }
    </div>
  }

}

export default CorporatePaymentReportDetailBar;
